<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'shop:sethome:shopAppsetHome:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-popconfirm
          title="是否确认发布首页？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handlePublishHome()"
        >
          <a-button type="primary" v-auth="'shop:sethome:shopAppsetHome:edit'">
            <Icon icon="simple-line-icons:screen-desktop" /> {{ t('发布首页') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.name }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />

    <BasicDrawer
      v-bind="$attrs"
      @register="registerAppsetHomeDrawer"
      :showFooter="false"
      width="90%"
      :maskClosable="true"
      :title="title"
    >
      <AppsetHome :homeId="homeId" :cversion="cversion" />
    </BasicDrawer>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopSethomeShopAppsetHomeList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { shopAppsetHomeDelete, shopAppsetHomeListData } from '/@/api/shop/sethome/shopAppsetHome';
  import {
    shopAppsetHomeDisable,
    shopAppsetHomeEnable,
    publishHome,
  } from '/@/api/shop/sethome/shopAppsetHome';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicDrawer } from '/@/components/Drawer';
  import AppsetHome from '../../appsetHome/list.vue';

  const [registerAppsetHomeDrawer, { openDrawer: openAppsetHomeDrawer }] = useDrawer();

  const { t } = useI18n('shop.sethome.shopAppsetHome');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('商城首页配置管理'),
  };

  let homeId = ref('');
  let cversion = ref('');
  let title = ref('');

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('配置描述'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('配置版本'),
        field: 'cversion',
        component: 'Input',
      },
      {
        label: t('发布状态'),
        field: 'fbStatus',
        component: 'Select',
        componentProps: {
          dictType: 'shop_fb_status',
          allowClear: true,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('配置描述'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 250,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('配置版本'),
      dataIndex: 'cversion',
      key: 'a.cversion',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('发布状态'),
      dataIndex: 'fbStatus',
      key: 'a.fb_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'shop_fb_status',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('修改时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('配置说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 150,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'simple-line-icons:home',
        title: t('首页展示配置'),
        onClick: handlehome.bind(this, record),
        auth: 'shop:sethome:shopAppsetHome:edit',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑商城首页配置'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:sethome:shopAppsetHome:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用商城首页配置'),
        popConfirm: {
          title: t('是否确认停用商城首页配置'),
          confirm: handleDisable.bind(this, { id: record.id }),
        },
        auth: 'shop:sethome:shopAppsetHome:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用商城首页配置'),
        popConfirm: {
          title: t('是否确认启用商城首页配置'),
          confirm: handleEnable.bind(this, { id: record.id }),
        },
        auth: 'shop:sethome:shopAppsetHome:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除商城首页配置'),
        popConfirm: {
          title: t('是否确认删除商城首页配置'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:sethome:shopAppsetHome:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: shopAppsetHomeListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handlehome(record: Recordable) {
    homeId.value = record.id;
    cversion.value = record.cversion;
    title.value = `首页配置 （版本号：${record.cversion}）`;
    openAppsetHomeDrawer(true);
    // console.log(record);
    // router.push({
    //   path: '/shop/appsetHome/list',
    //   // query: {
    //   //   id: record.id,
    //   //   code: record.code,
    //   //   name: record.name,
    //   // },
    // });
  }

  async function handleDisable(record: Recordable) {
    const res = await shopAppsetHomeDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await shopAppsetHomeEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await shopAppsetHomeDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 发布首页
  async function handlePublishHome() {
    const id = getSelectRowKeys();
    if (!id.length || id.length > 1) {
      showMessage(t('请选择一条需要发布的配置！'));
      return;
    }
    let params = {
      id: id[0],
    };
    const res = await publishHome(params);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
